package com.rong.tools.controller;

import com.rong.tools.config.ProjectConfig;
import com.rong.tools.entity.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author 周厚荣
 * @date 2025/4/26 10:18
 */
@RestController
@Slf4j
@RequestMapping("/file")
public class FileController extends ABaseController{
    @Autowired
    private ProjectConfig projectFolder;
    @PostMapping("/upload")
    public ResponseVO upload(@RequestParam("file")MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + ext;
        File dest = new File(projectFolder.getProjectFolder()+"icon/" + newFileName);
        dest.getParentFile().mkdirs();
        file.transferTo(dest);

        // 返回前端可用的路径
        String filePath = "icon/" + newFileName;
        return getSuccessResponseVO(filePath);
    }
}
